package com.zhuangfei.hos_timetableview.ability;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.config.OnMyConfigHandleAdapter;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.hos_timetableview.utils.ToastUtil;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.model.ScheduleConfig;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;

import java.util.List;
import java.util.Set;

public class LocalConfigAbility extends Ability {

    private static final String TAG = "BaseFuncActivity";

    // 控件
    TimetableView mTimetableView;
    Button moreButton;
    Text titleTextView;
    List<MySubject> mySubjects;
    String[] popupMenus;

    ScheduleConfig mScheduleConfig;
    Set<String> configSet;
    private static final String CONFIG_NAME = "local_config_name";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_color_pool);
        // 添加表头的include
        StackLayout stInclude = (StackLayout) findComponentById(ResourceTable.Id_st_include);
        stInclude.addComponent(LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_header_base_func, stInclude, false));
        moreButton = (Button) findComponentById(ResourceTable.Id_id_more);
        moreButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showPopmenu();
            }
        });
        mySubjects = SubjectRepertory.loadDefaultSubjects2();
        mySubjects.addAll(SubjectRepertory.loadDefaultSubjects());
        titleTextView = (Text) findComponentById(ResourceTable.Id_id_title);
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        mScheduleConfig = new ScheduleConfig(this);
        mScheduleConfig.setConfigName(CONFIG_NAME);
        // 获取控件
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .curTerm("大三下学期")
                .maxSlideItem(10)
                .monthWidthVp(30)
                .configName(CONFIG_NAME)
                .callback(new OnMyConfigHandleAdapter())
                .showView();
    }

    /**
     * 更新一下，防止因程序在后台时间过长（超过一天）而导致的日期或高亮不准确问题。
     *
     * @param intent 意图
     */
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        mTimetableView.onDateBuildListener().onHighLight();
    }

    /**
     * 显示弹出菜单
     */
    public void showPopmenu() {
        if (popupMenus == null) {
            popupMenus = new String[]{"加载本地配置", "清除本地配置", "导出配置至文本", "加载配置文本"};
        }
        new XPopup.Builder(getContext())
                .hasShadowBg(false)
                .atView(moreButton)
                .asAttachList(popupMenus, null,
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                switch (text) {
                                    case "加载本地配置":
                                        loadLocalConfig();
                                        break;
                                    case "清除本地配置":
                                        clearLocalConfig();
                                        break;
                                    case "导出配置至文本":
                                        exportLocalConfig();
                                        break;
                                    case "加载配置文本":
                                        loadLocalConfigSet();
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }, 0, 0).show();
    }

    /**
     * 设置本地配置项并使其生效
     */
    protected void loadLocalConfig() {
        mScheduleConfig.put(OnMyConfigHandleAdapter.CONFIG_SHOW_WEEKENDS, OnMyConfigHandleAdapter.VALUE_FALSE)
                .put(OnMyConfigHandleAdapter.CONFIG_SHOW_NOT_CUR_WEEK, OnMyConfigHandleAdapter.VALUE_TRUE)
                .put(OnMyConfigHandleAdapter.CONFIG_USERLESSS_COLOR, "#000000")
                .commit();
        mTimetableView.updateView();
    }

    /**
     * 清除本地配置项
     */
    protected void clearLocalConfig() {
        mScheduleConfig.clear();
        ToastUtil.showToast(this, "清除成功，下次进入生效!");
    }

    /**
     * 本地配置导出到文本
     */
    private void exportLocalConfig() {
        Set<String> set = mScheduleConfig.export();
        configSet = set;
        String content = "";
        for (String s : set) {
            content += s + "\n";
        }
        new XPopup.Builder(getContext())
                .asConfirm("配置导出", content,
                        "取消", "我知道了", null, null, false).show();
    }

    /**
     * 从文本导入本地配置
     */
    private void loadLocalConfigSet() {
        if (configSet == null) {
            new XPopup.Builder(getContext())
                    .asConfirm("配置导入", "还没有导出，先导出再来试试吧",
                            "取消", "我知道了", null, null, false).show();
        } else {
            mScheduleConfig.load(configSet);
            ToastUtil.showToast(this, "配置已生效");
        }
        mTimetableView.updateView();
    }
}
